package chapter04.h4_2.idcard;

import chapter04.h4_2.framework.Factory;
import chapter04.h4_2.framework.Product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IDCardFactoory extends Factory {
    private int idCount = 0;
    private Map<String, List<Integer>> owners = new HashMap<>();

    @Override
    protected Product createProduct(String owner) {
        idCount++;
        return new IDCard(owner, idCount);
    }

    @Override
    protected void registerProduct(Product product) {
        if (product instanceof IDCard) {
            List<Integer> list;
            if (owners.containsKey(((IDCard) product).getOwner())) {
                list = owners.get(((IDCard) product).getOwner());
                list.add(((IDCard) product).getId());
            } else {
                list = new ArrayList<>();
                list.add(((IDCard) product).getId());
                owners.put(((IDCard) product).getOwner(), list);
            }
        }

    }

    public Map<String, List<Integer>> getOwners() {
        return owners;
    }

    public void printOwners() {
        for (Map.Entry<String, List<Integer>> entry : owners.entrySet()) {
            System.out.print(entry.getKey() + ":");
            for (Integer i : entry.getValue()) {
                System.out.print(i+" ");
            }
            System.out.println();
        }
    }
}
